<template>
  <div class="login-container">
    <div class="login-card">
      <div class="logo-container">
        <h1 class="logo-text">BS Admin</h1>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <div class="title-container">
          <h3 class="title">{{ title }}</h3>
        </div>

        <el-form-item prop="username">
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="用户名"
            name="username"
            type="text"
            auto-complete="on"
            prefix-icon="User"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            ref="password"
            v-model="loginForm.password"
            placeholder="密码"
            name="password"
            :type="passwordType"
            auto-complete="on"
            prefix-icon="Lock"
            @keyup.enter="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <el-icon><View v-if="passwordType === 'password'" /><Hide v-else /></el-icon>
          </span>
        </el-form-item>

        <div class="remember-box">
          <el-checkbox v-model="loginForm.rememberMe">记住我</el-checkbox>
          <router-link class="forgot-link" to="/forgot-password">忘记密码？</router-link>
        </div>

        <el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px;" @click.prevent="handleLogin">登录</el-button>

        <div class="tips">
          <span>还没有账号? </span>
          <router-link to="/register">立即注册</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive, watch, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { useStore } from 'vuex';
import { ElMessage } from 'element-plus';
import { User, Lock, View, Hide } from '@element-plus/icons-vue';
import { validUsername } from '@/utils/validate';
import { getRememberMe } from '@/utils/auth';

export default {
  name: 'Login',
  components: {
    User,
    Lock,
    View,
    Hide
  },
  setup() {
    const router = useRouter();
    const route = useRoute();
    const store = useStore();
    const loading = ref(false);
    const passwordType = ref('password');
    const redirect = ref(undefined);
    const title = import.meta.env.VITE_APP_TITLE || 'BS Admin';
    
    const loginForm = reactive({
      username: '',
      password: '',
      rememberMe: getRememberMe()
    });
    
    // 监听路由变化
    watch(() => route.query.redirect, (value) => {
      redirect.value = value;
    }, { immediate: true });
    
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入用户名'));
      } else {
        callback();
      }
    };
    
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    
    const loginRules = {
      username: [{ required: true, trigger: 'blur', validator: validateUsername }],
      password: [{ required: true, trigger: 'blur', validator: validatePassword }]
    };
    
    const showPwd = () => {
      passwordType.value = passwordType.value === 'password' ? '' : 'password';
    };
    
    const handleLogin = () => {
      loading.value = true;
      store.dispatch('user/login', loginForm)
        .then(() => {
          router.push({ path: redirect.value || '/' });
          loading.value = false;
        })
        .catch(() => {
          loading.value = false;
        });
    };
    
    onMounted(() => {
      if (loginForm.username === '') {
        document.querySelector('input[name=username]').focus();
      } else if (loginForm.password === '') {
        document.querySelector('input[name=password]').focus();
      }
    });
    
    return {
      title,
      loginForm,
      loginRules,
      loading,
      passwordType,
      redirect,
      showPwd,
      handleLogin
    };
  }
};
</script>

<style lang="scss" scoped>
@use '@/styles/variables.scss' as vars;

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
  background-image: url('');

  .login-card {
    width: 400px;
    padding: 30px;
    margin: 0 auto;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }

  .logo-container {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 30px;

    .logo-text {
      color: vars.$primary-color;
      font-size: 24px;
      font-weight: bold;
    }
  }

  .title-container {
    position: relative;
    margin-bottom: 30px;

    .title {
      font-size: 20px;
      color: vars.$text-primary;
      margin: 0 auto 20px;
      text-align: center;
      font-weight: bold;
    }
  }

  .login-form {
    width: 100%;
    
    .el-form-item {
      margin-bottom: 20px;
    }
    
    .remember-box {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;
      
      .forgot-link {
        font-size: 14px;
        color: vars.$primary-color;
        text-decoration: none;
        
        &:hover {
          text-decoration: underline;
        }
      }
    }
  }

  .login-button {
    width: 100%;
    margin-top: 10px;
    margin-bottom: 20px;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: #889aa4;
    cursor: pointer;
  }

  .tips {
    font-size: 14px;
    color: vars.$text-regular;
    text-align: center;
    
    a {
      color: vars.$primary-color;
      text-decoration: none;
      
      &:hover {
        text-decoration: underline;
      }
    }
  }
}
</style> 